package com.wptk.filemanager.ui;

import android.os.Bundle;
import android.os.SystemClock;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;

import com.wptk.filemanager.BuildConfig;
import com.wptk.filemanager.R;
import com.wptk.filemanager.ad.AdLoadManager;
import com.wptk.filemanager.ad.utils.AnalyticsUtils;
import com.wptk.filemanager.model.FileDataManager;
import com.wptk.filemanager.ui.base.BaseActivity;
import com.wptk.filemanager.utils.LogUtils;
import com.wptk.filemanager.utils.ThreadManager;
import com.hz.sdka.ada.AdListenera;
import com.hz.sdka.ada.Interstitiala;
import com.hz.sdka.ada.Splasha;
import com.hz.sdka.commona.bean.HZAdError;

public class SplashADActivity extends BaseActivity {

    private FrameLayout adLayout;
    boolean showSplashAD = false;
    boolean showIntersAD = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.e(TAG, "oncreate");
        AnalyticsUtils.INSTANCE.logEvent("appqh_ad");

        if (FileDataManager.appConfig.getFile_Config().getAppqh_ad() == 0)
            loadSplashAD();
        else
            loadSplashAndIntersAD();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_splash_ad;
    }

    @Override
    protected void initEvent() {
        adLayout = findViewById(R.id.adLayout);
    }

    @Override
    protected void initView() {

    }


    private void loadSplashAndIntersAD() {
        LogUtils.e(TAG, "loadSplashAndIntersAD");
        ThreadManager.getInstance().execute(new Runnable() {
            @Override
            public void run() {

                for (int i = 0; i < 3; i++) {
                    LogUtils.e(TAG, "i = " + i);
                    if (Splasha.getInstance().isReady() && Interstitiala.getInstance().isReady()) {
                        if (Splasha.getInstance().getEcpm() > Interstitiala.getInstance().getEcpm()) {
                            showSplashAD = true;
                        } else {
                            showIntersAD = true;
                        }
                        whenLoadAD(i);
                        break;
                    }else if (Splasha.getInstance().isReady()) {
                        showSplashAD = true;
                        whenLoadAD(i);
                        break;
                    } else if (Interstitiala.getInstance().isReady()) {
                        showIntersAD = true;
                        whenLoadAD(i);
                        break;
                    }

                    if(i < 2){
                        SystemClock.sleep(2000);
                    }
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showAD();
                    }
                });
            }
        });
    }

    private void loadSplashAD() {
        LogUtils.e(TAG, "loadSplashAD");
        ThreadManager.getInstance().execute(new Runnable() {
            @Override
            public void run() {

                for (int i = 0; i < 3; i++) {
                    if (Splasha.getInstance().isReady()) {

                        whenLoadAD(i);
                        showSplashAD = true;
                        break;
                    }
                    if(i < 2){
                        SystemClock.sleep(2000);
                    }
                }

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showAD();
                    }
                });
            }
        });
    }

    private void whenLoadAD(int i) {

        String msg = "appqh_ad_ready_" + i*2 + "s";
        AnalyticsUtils.INSTANCE.logEvent(msg);
        showToast(msg);
        LogUtils.e(TAG, "whenLoadAD   i = " + i);
    }

    private void showAD() {
        LogUtils.e(TAG, "showSplashAD = " + showSplashAD + "  showIntersAD = " + showIntersAD);
        if (!showSplashAD && !showIntersAD) {
            afterAd();
            return;
        }

        if (showSplashAD) {
            AdLoadManager.INSTANCE.showSplashAd(this, adLayout, BuildConfig.CJ_SPLASH_AD, new AdListenera() {
                @Override
                public void adClose() {
                    afterAd();
                }

                @Override
                public void adError(HZAdError adError) {
                    afterAd();
                }

                @Override
                public void adShow() {
                    AnalyticsUtils.INSTANCE.logEvent("appqh_ad_succeed");
                }
            });
        }
        if (showIntersAD) {
            AdLoadManager.INSTANCE.showInterstitialAd(this, BuildConfig.CJ_SPLASH_AD, new AdListenera() {
                @Override
                public void adError(HZAdError adError) {
                    afterAd();
                }

                @Override
                public void adClose() {
                    afterAd();
                }

                @Override
                public void adShow() {
                    AnalyticsUtils.INSTANCE.logEvent("appqh_ad_succeed");
                }
            });
        }
    }

    private void afterAd() {
        LogUtils.e(TAG, "afterAd");
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LogUtils.e(TAG, "ondestory");
    }

}
